package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.ZhppDateUtil;
import com.zjdiepu.www.dao.dh.DhOverdueTrailerRecordMapper;
import com.zjdiepu.www.ibiz.dh.IDhOverdueTrailerRecordService;
import com.zjdiepu.www.model.dh.dto.DhOverdueTrailerRecord;
import com.zjdiepu.www.model.dh.ext.DhOverdueTrailerRecordExt;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月08日 15时30分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhOverdueTrailerRecordServiceImpl extends BaseService implements IDhOverdueTrailerRecordService {
	public static final Logger logger = LoggerFactory.getLogger(DhOverdueTrailerRecordServiceImpl.class);

	@Autowired
	private DhOverdueTrailerRecordMapper dhOverdueTrailerRecordMapper;
	
	@Override
	public void saveUpdateAndDelete(DhOverdueTrailerRecord dhOverdueTrailerRecord, OperateType operType) throws Exception {
		switch (operType) {
		case add:
			dhOverdueTrailerRecord.initInsertData(dhOverdueTrailerRecord.getModifier(), dhOverdueTrailerRecord.getModifierName());
			dhOverdueTrailerRecord.setCreated(ZhppDateUtil.dateConvertTimeStamp(dhOverdueTrailerRecord.getJbDate(),"yyyy-MM-dd"));
			dhOverdueTrailerRecordMapper.insert(dhOverdueTrailerRecord);
			break;
		case edit:
			dhOverdueTrailerRecord.initUpdateData(dhOverdueTrailerRecord.getModifier(), dhOverdueTrailerRecord.getModifierName());
			// 影响的记录数
			int updateCount = dhOverdueTrailerRecordMapper.updateByPrimaryKeySelective(dhOverdueTrailerRecord);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhOverdueTrailerRecord.initDeleteData(dhOverdueTrailerRecord.getModifier(), dhOverdueTrailerRecord.getModifierName());
			// 影响的记录数
			int deleteCount = dhOverdueTrailerRecordMapper.updateByPrimaryKeySelective(dhOverdueTrailerRecord);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhOverdueTrailerRecord> selectByExt(DhOverdueTrailerRecordExt dhOverdueTrailerRecordExt) {
		return dhOverdueTrailerRecordMapper.selectByExt(dhOverdueTrailerRecordExt);
	}
	
	@Override
	public int countByExt(DhOverdueTrailerRecordExt dhOverdueTrailerRecordExt){
		return dhOverdueTrailerRecordMapper.countByExt(dhOverdueTrailerRecordExt);
	}
	
	@Override
	public DhOverdueTrailerRecord selectById(String id) {
		return dhOverdueTrailerRecordMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhOverdueTrailerRecordMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhOverdueTrailerRecord dhOverdueTrailerRecord) {
		return dhOverdueTrailerRecordMapper.countRefined(dhOverdueTrailerRecord);
	}

	@Override
	public DhOverdueTrailerRecord selectRefined(DhOverdueTrailerRecord dhOverdueTrailerRecord) {
		return dhOverdueTrailerRecordMapper.selectRefined(dhOverdueTrailerRecord);
	}

	@Override
	public boolean repeat(DhOverdueTrailerRecord dhOverdueTrailerRecord) {
		int count = countRefined(dhOverdueTrailerRecord);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhOverdueTrailerRecord.getId())) {
			return true;
		}
		DhOverdueTrailerRecord dbItem = selectRefined(dhOverdueTrailerRecord);
		if(null == dbItem) {
			return false;
		}
		return !dhOverdueTrailerRecord.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhOverdueTrailerRecordMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
